<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<?php require 'hrfuns.php';
$db = GetDB();

function DisplayHeader()
{
    echo "<!-- Start of Header -->\n";
    echo "<tr>\n";
    echo "    <th rowspan=2 nowrap>工号\n";
    echo "    <th rowspan=2 nowrap>姓名\n";
    echo "    <th rowspan=2 nowrap>部门\n";
	echo "    <th rowspan=2 nowrap>班组\n";
    echo "    <th rowspan=2 nowrap>入职日期\n";
    echo "    <th rowspan=2 nowrap>类别（天）\n";
    echo "    <th colspan=12>年假/调休（1至12月）-小时\n";
    echo "    <th rowspan=2>2012休假总计（天）\n";
    echo "    <th rowspan=2>2011年底结余\n";
    echo "    <th rowspan=2>截止6月底2011假期结余\n";
    echo "    <th rowspan=2>2012年假总数\n";
    echo "    <th rowspan=2>截止12月底假期剩余（单项）\n";
    echo "    <th rowspan=2>截止12月底假期总计\n";
    echo "    <th colspan=11>其他调休\n";
    echo "</tr>\n";
    echo "<tr>\n";
    echo "    <th width=36>1月\n";
    echo "    <th width=36>2月\n";
    echo "    <th width=36>3月\n";
    echo "    <th width=36>4月\n";
    echo "    <th width=36>5月\n";
    echo "    <th width=36>6月\n";
    echo "    <th width=36>7月\n";
    echo "    <th width=36>8月\n";
    echo "    <th width=36>9月\n";
    echo "    <th width=36>10月\n";
    echo "    <th width=36>11月\n";
    echo "    <th width=36>12月\n";
    echo "    <th>三八妇女节\n";
    echo "    <th>青年节\n";
    echo "    <th>献血\n";
    echo "    <th>本月病假\n";
    echo "    <th>总计病假\n";
    echo "    <th>本月事假\n";
    echo "    <th>总计事假\n";
    echo "    <th>婚/丧假\n";
    echo "    <th>产假/哺乳假\n";
    echo "    <th>其他\n";
    echo "    <th>工时平衡\n";
    echo "</tr>\n";
    echo "<!-- End of header -->\n";
}

function GetVaDataRs($type, $emid)
{
    global $db;
    $sqlstr = "select va_date, va_length from va_t"
            . " where va_type_id = " . $type . " and em_id = '" . $emid . "'";

//    echo $sqlstr;
    $rs = $db->query($sqlstr);
    return $rs;
}

function GetVaLastRs($emid)
{
    global $db;
    $sqlstr = "select va_1_length, va_2_length, va_3_length from valast_t"
            . " where em_id = '" . $emid . "'";

    $rs = $db->query($sqlstr);
    return $rs;
}

function GetVaThisRs($emid)
{
    global $db;
    $sqlstr = "select va_1_length, va_2_length from vathis_t"
            . " where em_id = '" . $emid . "'";
    $rs = $db->query($sqlstr);
    return $rs;
}

function GetVaRs($type, $emid)
{
    global $db;
    $sqlstr = "select va_date, va_length from va_v"
            . " where va_type_id=" . $type . " and em_id = '" . $emid . "'";
    $rs = $db->query($sqlstr);
    return $rs;
}

function GetVaSum($type, $emid)
{
    global $db;
    $sqlstr = "select sum(va_length) from va_t"
            . " where va_type_id=" . $type . " and em_id = '" . $emid . "'";
    $rs = $db->query($sqlstr);
    $row = $rs->fetchArray();
    return $row[0];
}

function DisplayOnePerson($rowEm)
{
    $today = new DateTime();

    # First line
    printf("<!-- First line -->\n");
    printf("<tr>\n");
    printf("    <td rowspan=4 nowrap>" . $rowEm["em_id"] . "\n");
    printf("    <td rowspan=4 nowrap>" . $rowEm["em_name"] . "\n");
    printf("    <td rowspan=4>" . $rowEm["em_depart"] . "\n");
	printf("    <td rowspan=4>" . $rowEm["em_class"] . "\n");
    printf("    <td rowspan=4 nowrap>" . $rowEm["em_enter"] . "\n");
    printf("    <td nowrap>法定年休\n");

    $va_1_Data = array(0,0,0,0,0,0,0,0,0,0,0,0);
    $va_1_Total = 0;

    $rsVa = GetVaDataRs(1, $rowEm["em_id"]); //法定休假
    while($rowVa = $rsVa->fetchArray()) {
        $date = new DateTime($rowVa["va_date"]);
        $va_1_Data[$date->format('n')-1] += $rowVa["va_length"];
        $va_1_Total += $rowVa["va_length"];
    } # while ($rowVa

    for ($i=0; $i < 12; $i++) {
        if ($va_1_Data[$i] != 0 ) {
            printf("    <td rowspan=2 class=number_td><font color=red>%.1f</font>\n", $va_1_Data[$i]);
        }
        else {
            printf("    <td rowspan=2>&nbsp;\n");
        }
    }
    printf("    <td rowspan=2 class=number_td><font color=red>%.1f</font>\n", $va_1_Total/8);

    $rsVaLast = GetVaLastRs($rowEm["em_id"]);

    $rowVaLast = $rsVaLast->fetchArray();

    $va_3_Data = array(0,0,0,0,0,0,0,0,0,0,0,0);
    $va_3_Total = 0;
    $rsVa = GetVaDataRs(3, $rowEm["em_id"]);
    while($rowVa = $rsVa->fetchArray()) {
        $date = new DateTime($rowVa["va_date"]);
        $va_3_Data[$date->format('n')-1] += $rowVa["va_length"];
        $va_3_Total += $rowVa["va_length"];
    } # while ($rowVa

    $va_13_Data = array(0,0,0,0,0,0,0,0,0,0,0,0);
    $va_13_Total = 0;
    $rsVa = GetVaDataRs(13, $rowEm["em_id"]);
    while($rowVa = $rsVa->fetchArray()) {
        $date = new DateTime($rowVa["va_date"]);
        $va_13_Data[$date->format('n')-1] += $rowVa["va_length"];
        $va_13_Total += $rowVa["va_length"];
    } # while ($rowVa

    //至年中法定休假
    $va_1_Middle = $rowVaLast["va_1_length"] + $va_1_Total/8 + $va_3_Total/8;    //至年中福利休假
    $va_2_Middle = $va_1_Middle > 0 ? $rowVaLast["va_2_length"]
                                : $rowVaLast["va_2_length"] + $va_1_Middle;
    //至年中调休
    $va_3_Middle = $va_2_Middle > 0 ? $rowVaLast["va_3_length"]
                                    : $rowVaLast["va_3_length"] + $va_2_Middle;

    $rsVaThis = GetVaThisRs($rowEm["em_id"]);

    $rowVaThis = $rsVaThis->fetchArray();

    $va_other_Total = 0;//妇女节，献血，青年节

    for ($type = 4; $type < 6; $type++) {
        $sum = GetVaSum($type, $rowEm["em_id"]);
        $va_other_Total += $sum/8;
    }
    //至年底法定休假
    $va_1_Final = $va_3_Middle > 0 ? $rowVaThis["va_1_length"]
                                   : $rowVaThis["va_1_length"] + $va_3_Middle;
    //至年底福利休假
    $va_2_Final = $va_1_Final > 0 ? $rowVaThis["va_2_length"]
                                  : $rowVaThis["va_2_length"] + $va_1_Final;
    $va_2_Final += $va_other_Total;
    //至年底新增可用调休
    $va_3_Final = $va_2_Final > 0 ? $va_13_Total/8
                                  : $va_13_Total/8 + $va_2_Final;

    printf("    <td>%.1f\n", $rowVaLast["va_1_length"]);
    printf("    <td>%.1f\n", $va_1_Middle > 0 ? $va_1_Middle : 0);
    printf("    <td>%.1f\n", $rowVaThis["va_1_length"]);
    printf("    <td>%.1f\n", $va_1_Final > 0 ? $va_1_Final : 0);

    //至年底可用假期
    $va_total_Final = ($va_1_Final > 0 ? $va_1_Final : 0)
                      + ($va_2_Final > 0 ? $va_2_Final : 0)
                      + $va_3_Final
                      + ($va_3_Middle > 0 ? $va_3_Middle : 0)
                      ;

//    if ($today->format("n") <= 6)
//        $va_total_Final  += ($va_2_Middle > 0 ? $va_2_Middle : 0)
//                          + ($va_1_Middle > 0 ? $va_1_Middle : 0);

    if ($va_total_Final < 2)
	    printf("    <td rowspan=4 bgcolor=#ff8080>%.1f\n", $va_total_Final);
	else
	    printf("    <td rowspan=4>%.1f\n", $va_total_Final);

    for ($type = 4; $type < 13; $type++) {
        $sum = GetVaSum($type, $rowEm["em_id"]);
        if ($type == 7 || $type == 8) {  //病假，事假
            $rs = GetVaRs($type, $rowEm["em_id"]);
            $vaTotal = 0;
            while ($row = $rs->fetchArray()) {
                $date = new DateTime($row["va_date"]);

                if ($date->format('n') == $today->format('n')-1)
                    $vaTotal += $row["va_length"];
            }
            if ($vaTotal != 0)
                printf("    <td rowspan=4>%.1f\n", $vaTotal/8);
            else
                printf("    <td rowspan=4>&nbsp;\n");
        }
        if ($sum != 0) {
            printf("    <td rowspan=4>%.1f\n", $sum/8);
        }
        else { printf("    <td rowspan=4>&nbsp;\n"); }
    }

    printf("</tr>\n");

    # Second line
    printf("<!-- Second line -->\n");
    printf("<tr>\n");
    printf("    <td>福利年休\n");
    printf("    <td>%.1f\n", $rowVaLast["va_2_length"]);

    printf("    <td>%.1f\n", $va_2_Middle > 0 ? $va_2_Middle : 0);
    printf("    <td>%.1f\n", $rowVaThis["va_2_length"]);

    printf("    <td>%.1f\n", $va_2_Final > 0 ? $va_2_Final : 0);
    printf("</tr>\n");

    # Third line

    printf("<!-- Third line -->\n");
    printf("<tr>\n");
    printf("<td>调休\n");

    for ($i=0; $i < 12; $i++) {
        if ($va_3_Data[$i] < 0 ) {
            printf("    <td class=number_td><font color=red>%.1f</font>\n", $va_3_Data[$i]);
        }
        else {
            printf("    <td class=number_td>&nbsp;\n");
        }
    } # for
    printf("    <td><font color=red>%.1f</font>\n", $va_3_Total/8);
    printf("    <td>%.1f\n", $rowVaLast["va_3_length"]);

    printf("    <td>%.1f\n", $va_3_Middle > 0 ? $va_3_Middle : 0);
    printf("    <td>0.0\n");
    printf("    <td>%.1f\n", $va_3_Middle > 0 ? $va_3_Middle : 0);
    printf("</tr>\n");

    # Fourth Line
    printf("<!-- Fourth line -->\n");
    printf("<tr>\n");
    printf("    <td nowrap>本年新增调休\n");

    for ($i=0; $i < 12; $i++) {
        if ($va_13_Data[$i]>0) {
            printf("    <td class=number_td>%.1f\n", $va_13_Data[$i]);
        }
        else {
            printf("    <td class=number_td>&nbsp;\n");
        }
    }

    printf("    <td>&nbsp\n");
    printf("    <td>&nbsp\n");
    printf("    <td>&nbsp\n");
    printf("    <td>%.1f\n", $va_13_Total/8);
    printf("    <td>%.1f\n", $va_3_Final);
}
?>

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>Vacation detail report</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="hr.css" />
</head>
<body>

<table bgcolor=#6f6f6f cellspacing=1 cellpadding=2 border=0 width=1440>
<?php

DisplayHeader();

$sqlstr = "select em_id, em_name, em_depart_id, em_depart, em_enter, em_class"
        . " from eminfo_v where em_depart_id < 100";

if (isset($_POST["submit"])) {
    if ($_POST['selEmID'] != "all") {
        $sqlstr .= " and em_id = '" . $_POST["selEmID"] . "'";
    }

    if ($_POST['selDepart'] != 0) {
        $sqlstr .= " and em_depart_id = " . $_POST["selDepart"];
    }

    if ($_POST["selClass"] != "all") {
        $sqlstr .= " and em_class = '" . $_POST["selClass"] . "'";
    }
}

$sqlstr .= " order by em_id";

//echo $sqlstr;
$rsEm = $db->query($sqlstr);

while($rowEm = $rsEm->fetchArray()) {
    DisplayOnePerson($rowEm);
}
?>

</tr>
</table>

</body>
</html>
<?php $db->close(); ?>
